package com.godme.structure;

import com.godme.structure.LinkedList.LinkedNode;

public class Stack {
    private LinkedNode head = new LinkedNode(-1);

    public int pop(){
        if(head.next == null){
            throw new RuntimeException("stack is empty");
        }
        LinkedNode first = head.next;
        head.next = null;
        if(first.next != null){
            head.next = first.next;
        }
        return first.value;
    }

    public void push(int value){
        LinkedNode insertNode = new LinkedNode(value);
        insertNode.next = head.next;
        head.next = insertNode;
    }

    public boolean isEmpty(){
        return head.next == null;
    }

    public void show(){
        StringBuilder sb = new StringBuilder("[");
        LinkedNode cursor = head.next;
        while (cursor != null){
            sb.append(cursor.value);
            if(cursor.next != null){
                sb.append("->");
            }
            cursor = cursor.next;
        }
        System.err.println(sb.append("]"));
    }


    public static void main(String[] args) {
        Stack stack = new Stack();
        for (int i = 0; i < 5; i++) {
            stack.push(i);
            stack.show();
        }
        System.err.println("=========");
        while (!stack.isEmpty()){
            stack.pop();
            stack.show();
        }

    }




}
